#python -m data.api_data.api_alarm   文件直接执行代码
from .api_token import get_id,get_time,get_token
from util.time_util import timestran
import requests
import json
with open(r'data\json_data\alarm.json','r',encoding='utf-8') as f:
    data = json.load(f)['api']

def get_alarmlist(type = 1):
    values =[]
    url = 'http://hxzhan.rtcloud.com:32111/tm/alarm/list'
    headers = {
        'content-type':'application/json; charset=utf-8',
        'Tm-Token':get_token(), 
        'rt-request-ts':get_time(),
        'rt-request-id':get_id()
    }
    json = {'page':1,'limit':10,'device_id':57714,'scene_id':137}
    response = requests.post(url=url,headers=headers,json=json)
    if type == 1:
        key_list = data['alarmlist']
    else:
        key_list = data['details']
    data_list = response.json()['data']['list']
    for elm in data_list:
        for key in key_list:
            if key == 'alarm_at' or key == 'return_at':
                values.append(timestran(elm[key]))
            elif key == 'level':
                if elm[key] == 1:
                    values.append('预警')
                elif elm[key] == 2:
                    values.append('报警')
            elif key == 'return_status':
                if elm[key] == 1:
                    values.append('已复归')
                elif elm[key] == 2:
                    values.append('未复归')
            elif key == 'confirm_status':
                if elm[key] == 1:
                    values.append('已确认')
                elif elm[key] == 2:
                    values.append('未确认')
            else:
                values.append(elm[key])
        if type != 1:
            break
    return values

